LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;

ENTITY Welcome IS
	PORT(
		Clk : IN std_logic;
		Start : IN std_logic;
		Finish : OUT std_logic;
		Reset : IN std_logic;
		
		GameMode : OUT std_logic_vector(1 downto 0);
		
		PrimAddr    : OUT std_logic_vector(9 downto 0);
		PrimWriteData    : OUT  std_logic_vector(15 downto 0);
		PrimWriteEnable  : OUT std_logic;

		RawP1Key : IN std_logic_vector(5 downto 0);
		CurSelect : OUT std_logic_vector(1 DOWNTO 0);
		
		DebugSignal : OUT std_logic_vector(2 DOWNTO 0)
	--	DirState : IN std_logic_vector(2 downto 0)
	);
END Welcome;

ARCHITECTURE rtl OF Welcome IS

	TYPE State_Type IS (Idle, GotoNext, Readkey, Ready);
	
	SIGNAL state : State_Type;
	SIGNAL sCounter : std_logic_vector(3 downto 0);

	SIGNAL PosX, PosY : std_logic_vector(4 downto 0);
	
	
	TYPE KeyCounter IS ARRAY(5 DOWNTO 0) OF std_logic_vector(17 DOWNTO 0);
	SIGNAL P1KeyCounter : KeyCounter;
	SIGNAL P1KeyDir : std_logic_vector(2 downto 0);
	SIGNAL P1KeyFire : std_logic;
	SIGNAL CurSel : std_logic_vector(1 DOWNTO 0);
BEGIN

	P1KeyFire <= '1' WHEN P1KeyCounter(4) = 1 ELSE '0';
	P1KeyDir  <= "000" WHEN P1KeyCounter(3) = 1 ELSE
				 "001" WHEN P1KeyCounter(2) = 1 ELSE
				 "010" WHEN P1KeyCounter(1) = 1 ELSE
				 "011" WHEN P1KeyCounter(0) = 1 ELSE
				 "100";

	CurSelect <= CurSel;
	
	DebugSignal <= P1KeyDir;

	PROCESS(Clk, Reset)
	BEGIN
		IF Reset = '0' THEN
			state <= Idle;
			Finish <= '0';
			GameMode <= "00";
		ELSIF rising_edge(Clk) THEN
			CASE state IS
				WHEN Idle =>
					IF Start = '1' THEN
						state <= ReadKey;
					ELSE
						state <= Idle;
						GameMode <= "00";
					END IF;
					Finish <= '0';
					P1KeyCounter <= (OTHERS => (OTHERS => '0'));
					CurSel <= "01";
				WHEN ReadKey =>
						----- UpdateKey
							IF RawP1Key(0) = '1' THEN
								P1KeyCounter(0) <= P1KeyCounter(0) +1;
							ELSE
								P1KeyCounter(0) <= (OTHERS => '0');
							END IF;
							IF RawP1Key(1) = '1' THEN
								P1KeyCounter(1) <= P1KeyCounter(1) +1;
							ELSE
								P1KeyCounter(1) <= (OTHERS => '0');
							END IF;
							IF RawP1Key(2) = '1' THEN
								P1KeyCounter(2) <= P1KeyCounter(2) +1;
							ELSE
								P1KeyCounter(2) <= (OTHERS => '0');
							END IF;
							IF RawP1Key(3) = '1' THEN
								P1KeyCounter(3) <= P1KeyCounter(3) +1;
							ELSE
								P1KeyCounter(3) <= (OTHERS => '0');
							END IF;
							IF RawP1Key(4) = '1' THEN
								P1KeyCounter(4) <= P1KeyCounter(4) +1;
							ELSE
								P1KeyCounter(4) <= (OTHERS => '0');
							END IF;
							IF RawP1Key(5) = '1' THEN
								P1KeyCounter(5) <= P1KeyCounter(5) +1;
							ELSE
								P1KeyCounter(5) <= (OTHERS => '0');
							END IF;
						---------------------------------------------------------------------
						--END GENERATE;
						IF P1KeyFire = '1' THEN
							state <= GotoNext;
						ELSE
							IF P1KeyDir(2) = '0' THEN
								IF P1KeyDir(1 DOWNTO 0) = "01" THEN
									IF CurSel = "11" THEN
										CurSel <= "01";
									ELSE
										CurSel <= CurSel +1;
									END IF;
								ELSIF P1KeyDir(1 DOWNTO 0) = "00" THEN
									IF CurSel = "01" THEN
										CurSel <= "11";
									ELSE
										CurSel <= CurSel -1;
									END IF;
								ELSE
									state <= ReadKey;
								END IF;
							ELSE
								state <= ReadKey;
							END IF;
						END IF;
				WHEN GotoNext =>
						Finish <= '1';
						GameMode <= CurSel;
						state <= Ready;
				WHEN Ready =>
					state <= Idle;
				WHEN OTHERS =>
					NULL;
			END CASE;															
		END IF;
	END PROCESS;
END rtl;
